package com.gnkexy.springframework.test.bean;

import com.gnkexy.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class ProxyBeanFactory implements FactoryBean<IUserDao> {
    @Override
    public IUserDao getObject() {
        return (IUserDao) Proxy.newProxyInstance(
                ProxyBeanFactory.class.getClassLoader(),
                new Class[]{IUserDao.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Map<String, String> Map = new HashMap<>();
                        Map.put("0001", "yhz");
                        Map.put("0002", "dww");
                        Map.put("0003", "ad");
                        System.out.println("你被代理了!!! ");
                        return Map.get(args[0].toString());
                    }
                });
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
